import { createStore } from 'vuex'
import {getAllTasks, getMessages, getRoleList, setMessage, updateTask} from "@/request/api";

export default createStore({
  state: {
    userList:[
      {
        'role':'boss',
        'permission':{
          'list':['我的职位','我的消息','发布消息']
        },
        'user':[
            {
              'name':'adel',
              'id':'1000',
              'pwd':'12345678',
              'work':[0,]
            }
        ]
      },
      {
        'role': 'runner',
        'permission':{
          'list':['我的职位','我的消息']
        },
        'user':[
          {
            'name':'张三',
            'id': '0001',
            'pwd':'123123',
            'work':[1,2]
          }
        ]
      },
      {
        'role': 'repair',
        'permission':{
          'list':['我的职位','我的消息']
        },
        'user':[
          {
            'name': '小张',
            'id': '0011',
            'pwd':'123456',
            'work': [3,4]
          }
        ]
      }
    ],
    workList:[
      {
        id: 0,
        address:'老板办公室',
        detail:'监管工人的工作，查看工单处理过程，发布消息'
      },
      {
        id: 1,
        address:'产线A',
        detail:'负责A产线上设备的运维，定时检查设备工作状态，出现异常及时报修'
      },
      {
        id: 2,
        address:'产线B',
        detail:'负责B产线上设备的运维，定时检查设备工作状态，出现异常及时报修'
      },
      {
        id: 3,
        address:'产线A',
        detail:'负责A产线上设备的维修，接受保修工单，去现场排查及检修'
      },
      {
        id: 4,
        address:'产线B',
        detail:'负责B产线上设备的维修，接受保修工单，去现场排查及检修'
      }
    ],
    roleList:[],
    currentUser:{
      name: localStorage.getItem("name") || "",
      ID: localStorage.getItem("ID") || "",
      phone:localStorage.getItem("phone") || "137777777",
      image:localStorage.getItem("image") || "",
      role: localStorage.getItem("role") || [],
      work: localStorage.getItem("work") || "",
      permission: localStorage.getItem("permission") || ""
    },
    currentPage:{
      LineName: localStorage.getItem('LineName') || '',
      LineID:   localStorage.getItem('LineID') || '',
      DeviceName: localStorage.getItem('DeviceName') ||'',
      DeviceID: localStorage.getItem('DeviceID') || '',
      active: localStorage.getItem("active")|| 0,
      devicesData: [],
      deviceData: []
    },
    productionLine:[
      {
        name: '产线A',
        id:'001',
        src: "https://img1.baidu.com/it/u=793856324,1901551201&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=375",
        devices:[
          {
            deviceID: '0001',
            name: '电机1',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            deviceID: '0002',
            name: '电机2',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            deviceID: '0003',
            name: '电机3',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            deviceID: '0004',
            name: '电机4',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            deviceID: '0005',
            name: '机械臂1',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            deviceID: '0006',
            name: '机械臂2',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
        ]
      },
      {
        name: '产线B',
        id: '002',
        src: 'https://img2.baidu.com/it/u=2129361999,2506785556&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500',
        devices:[
          {
            name:'电机1',
            deviceID:'0007',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          },
          {
            name: '电机2',
            deviceID: '0008',
            status:'工作中',
            data:[
              {
                'type':'temperature',
                'current':'45',
                'normal': '40'
              },
              {
                'type':'noise',
                'current':'65',
                'normal': '60'
              },
              {
                'type':'Vibration',
                'current':'350',
                'normal': '340'
              },
              {
                'type': 'speed',
                'current':'150',
                'normal': '140'
              }
            ]
          }
        ]
      }
    ],
    taskList: [
      {
        type: '标题1',
        details: [
              {
                label: '工单号',
                value: '值1',
                span: 1,
                display: true,
                edible:false
              },
              {
                label: '工单名称',
                value: '值1',
                span: 1,
                display: false,
                edible:true
              },
              {
                label: '创建时间',
                value: '值5',
                span: 2,
                display: false,
                edible:false
              },
              {
                label: '发起人',
                value: '1000',
                span: 2,
                display: false,
                edible:false
              },
              {
                label: '设备名称',
                value: '值2',
                span: 1,
                display: true,
                edible:false
              },
              {
                label: '设备ID',
                value: '值3',
                span: 1,
                display: false,
                edible:false
              },
              {
                label: '设备位置',
                value: '值4',
                span: 2,
                display: false,
                edible:false
              },
              {
                label: '设备状态',
                value: '值5',
                span: 2,
                display: true,
                edible:true
              },
              {
                label: '故障类型',
                value: '值5',
                span: 2,
                display: true,
                edible:true
              },
              {
                label: '工单类型',
                value: '值5',
                span: 2,
                display: true,
                edible:true
              },
              {
                label: '维修建议',
                value: '值5',
                span: 2,
                display: false,
                edible:true
              },
              {
                label: '处理进度',
                value: '值5',
                span: 2,
                display: false,
                edible:false
              },
              {
                label: '处理人',
                value: '值5',
                span: 2,
                display: false,
                edible:false
              },
              {
                label: '工单状态',
                value: 1,
                span: 2,
                display: false,
                edible:false
              }
            ]
      }
    ],
    taskLength:{
      all:0,
      finished:0,
      doing:0,
      submitted:0,
      mine:0
    },
    messages: localStorage.getItem("messages") || [],
    isLogin:sessionStorage.getItem('isLogin') || false
  },
  getters: {
  },
  mutations: {
    login(state, {role,user,id,work,permission}){
      console.log('mutations/login',role,user,id,work,permission);
      state.currentUser.name = user;
      state.currentUser.ID = id;
      state.currentUser.role = role;
      state.currentUser.permission = permission;
      state.currentUser.work = work;
      localStorage.setItem('role',JSON.stringify(role));
      localStorage.setItem('name',user);
      localStorage.setItem('ID',id);
      localStorage.setItem('permission',JSON.stringify(permission))
      localStorage.setItem('work',JSON.stringify(work))

      //登陆的状态变量，后端实现以后可以存储token
      sessionStorage.setItem('isLogin','true')
    },
    setLineName(state, {LineName,LineID}){
      console.log('saving...',LineName,LineID)
      state.currentPage.LineName = LineName;
      state.currentPage.LineID = LineID;
      localStorage.setItem('LineName',LineName);
      localStorage.setItem('LineID',LineID);
    },
    setDeviceName(state, {DeviceID,DeviceName}){
      console.log('saving...',DeviceID,DeviceName);
      //vuex存储
      state.currentPage.DeviceID = DeviceID;
      state.currentPage.DeviceName = DeviceName;
      //localstorage存储
      localStorage.setItem('DeviceID',DeviceID);
      localStorage.setItem('DeviceName',DeviceName);
    },
    setDeviceData(state, data){
      console.log('saving...',data)
      //vuex存储
      state.currentPage.deviceData = data
      localStorage.setItem('deviceData',JSON.stringify(data))
    },
    setDevices(state, data){
      // console.log('saving...', data);
      state.currentPage.devicesData = data;
    },
    setActiveNumber(state,num){
      localStorage.setItem("active",num);
      state.currentPage.active = num;
    },
    quitLogin(state){
      console.log('quitLogin')
      // localStorage.clear()
      //清空当前用户信息
      localStorage.setItem("name","")
      localStorage.setItem("ID","")
      localStorage.setItem("phone","")
      localStorage.setItem("image","")
      localStorage.setItem("role","")
      localStorage.setItem("work","")
      localStorage.setItem("permission","")
      localStorage.setItem('work',"")
      //清空当前产线，当前设备等数据的本地信息
      localStorage.setItem('LineName',"")
      localStorage.setItem('LineID',"")
      localStorage.setItem('DeviceName',"")
      localStorage.setItem('DeviceID',"")
      localStorage.setItem("active","")

      state.name=""
      state.currentUser.ID=""
      state.currentUser.phone="137777777"
      state.currentUser.image=""
      state.currentUser.role=""
      state.currentUser.work=""
      state.currentUser.permission=""

      //改变登录状态
      sessionStorage.setItem('isLogin','false')
      localStorage.removeItem('token')
    },
    SendMessage(state,xx){
      console.log('SendMessage-commit',xx)
      //vuex存储一份
      state.messages.push(xx)
      // 本地存储一份
      localStorage.setItem("messages",JSON.stringify(state.messages))

      //上传到数据库一份
      setMessage(JSON.stringify(xx)).then((res)=>{
        console.log('SendMessage:',res)
      })
    },
    addTask(state,task){
      console.log('addTask ',task)
      state.taskList.push(task)
    },

    getAllTask(state){
      state.taskLength.all = 0
      state.taskLength.finished = 0
      state.taskLength.doing = 0
      state.taskLength.submitted = 0
      state.taskLength.mine = 0
      getAllTasks().then((r) => {
          console.log('getAllTasks',r)
          if(r.code === 200) {
            //更新一下vuex里的所有工单

            state.taskList = r.data.tasks
            // console.log(state.taskList)

            //保存所有工单的数量
            state.taskLength.all = r.data.length

            r.data.tasks.forEach((s) => {
              // console.log(s)
              // console.log(s.details[12].value)
              //判断工单的处理进度，统计数量
              switch (s.details[13].value) {
                case 1:
                  state.taskLength.submitted += 1;
                  break;
                case 2:
                  state.taskLength.doing += 1;
                  break;
                case 3:
                  state.taskLength.finished += 1;
                  break;
                default :
                  break;
              }
              //统计自己发起的工单数量
              if(s.details[3].value === state.currentUser.ID || s.details[12].value === state.currentUser.ID){
                state.taskLength.mine += 1;
              }
            })
          }

      })
    },
    upDateTaskLength(state) {

      console.log('updateTaskLength')
      state.taskLength.finished = 0
      state.taskLength.doing = 0
      state.taskLength.submitted = 0
      state.taskLength.mine = 0

      state.taskList.forEach((s) => {
        // console.log(s)
        // console.log(s.details[12].value)
        //判断工单的处理进度，统计数量
        switch (s.details[13].value) {
          case 1:
            console.log('submitted')
            state.taskLength.submitted += 1;
            break;
          case 2:
            console.log('doing')
            state.taskLength.doing += 1;
            break;
          case 3:
            console.log('finished')
            state.taskLength.finished += 1;
            break;
          default :
            console.log('啥也不是')
            break;
        }
        //统计自己发起的工单数量
        if(s.details[3].value === state.currentUser.ID || s.details[12].value === state.currentUser.ID){
          console.log('我处理的或者我发起的')
          state.taskLength.mine += 1;
        }
      })
      console.log(state.taskLength)
    },
    getRoleList(state){
      getRoleList().then((res)=>{
        console.log('getRoleList:',res)
        var list = []

        res.data.forEach((i)=>{
          let role = {
            id:i[0],
            name:i[1],
            work:JSON.parse(i[2])
          }
          list.push(role)
        })
        // console.log(list)
        state.roleList = list
      })
    },
    getMessages(state){
      getMessages().then((res) => {
        console.log('getAllMessages',res)
        if(res.code === 200){
          // console.log(res.data)
          state.messages = res.data
          localStorage.setItem('messages',JSON.stringify(res.data))
        }
      })
    },
    updateTask(state,task){
      console.log('updateTask',task)
      // console.log(state.taskList)
      state.taskList.forEach((i)=>{
        if(i.id === task.id){
          i = task
        }
      })
      // console.log(state.taskList)

      //向服务器提交一份
      updateTask(task.id,JSON.stringify(task)).then((res)=>{
        console.log('updateTask',res)
      })
    }
  },
  actions: {
  },
  modules: {
  }
})
